﻿using System;
using System.Collections.Generic;
using HiTeachers.Data.Manager;
using HiTeachers.Entities;
using HiTeachers.Interfaces;
using HiTeachers.Utilities;
using NHibernate;

namespace HiTeachers.Data
{
    public class LoggerEmailDao : AbstractNHibernateDao<LoggerEmail, Guid>, ILoggerEmailDao
    {
        public bool IsExistEmail(string emailAddress, EmailType emailType)
        {
            try
            {
                const string hsql = "from LoggerEmail le where le.Email = :email and le.Type = :emailType";

                IQuery query = NHibernateSession.CreateQuery(hsql);

                query.SetString("email", emailAddress);
                query.SetInt32("emailType", Convert.ToInt32(emailType));

                return (query.UniqueResult<LoggerEmail>() != null);
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return false;
            }
        }

        public IList<LoggerEmail> GetSubcribeEmails()
        {
            try
            {
                const string hsql = "from LoggerEmail le where le.IsConfirmed = 1 and le.Type = :emailType";

                IQuery query = NHibernateSession.CreateQuery(hsql);
                query.SetInt32("emailType", Convert.ToInt32(EmailType.Subcribe));

                return query.List<LoggerEmail>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<LoggerEmail>();
            }
        }

        public int TotalNumberOfLoggerEmail(EmailType emailType)
        {
            try
            {
                const string hsql = "select count(*) from LoggerEmail le where le.Type = :emailType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetInt32("emailType", Convert.ToInt32(emailType));

                return Convert.ToInt32(query.UniqueResult());
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return 0;
            }
        }

        public IList<LoggerEmail> GetLoggerEmails(EmailType emailType)
        {
            try
            {
                const string hsql = "from LoggerEmail le where le.Type = :emailType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetInt32("emailType", Convert.ToInt32(emailType));

                return query.List<LoggerEmail>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<LoggerEmail>();
            }
        }

        public IList<LoggerEmail> GetLoggerEmails(EmailType emailType, int maximumRows, int startRowIndex)
        {
            try
            {
                const string hsql = "from LoggerEmail le where le.Type = :emailType";

                var query = NHibernateSession.CreateQuery(hsql);
                query.SetInt32("emailType", Convert.ToInt32(emailType));

                query.SetMaxResults(maximumRows);
                query.SetFirstResult(startRowIndex);

                return query.List<LoggerEmail>();
            }
            catch (Exception ex)
            {
                Logger.Debug(ex.Message);
                return new List<LoggerEmail>();
            }
        }
    }
}
